<template>
  <div>
    <el-upload
      class="upload-demo"
      list-type="picture-card"
      :action="upurl"
      :before-remove="beforeRemove"
      :on-preview="handlePictureCardPreview"
      :limit="20"
      :on-exceed="handleExceed"
      :on-success="success"
      multiple
      ref="upload"
    >
      <i class="el-icon-plus avatar-uploader-icon"></i>
    </el-upload>
    <el-dialog :visible.sync="dialogVisible">
      <img width="100%" :src="dialogImageUrl" alt="" />
    </el-dialog>
    <el-main
      ><el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
        style="width: 560px; margin: 0 auto"
      >
        <el-form-item label="商品标题:" prop="title">
          <el-input v-model="ruleForm.title"></el-input>
        </el-form-item>
        <el-form-item label="商品介绍:" prop="commodity">
          <el-input v-model="ruleForm.commodity" type="textarea"></el-input>
        </el-form-item>
        <el-form-item
          label="商品价格:"
          prop="price"
          style="width: 278px; display: inline-block"
        >
          <el-input v-model.number="ruleForm.price"></el-input>
        </el-form-item>
        <el-form-item
          label="商品库存:"
          prop="inventory"
          style="width: 278px; display: inline-block"
        >
          <el-input v-model.number="ruleForm.inventory"></el-input>
        </el-form-item>
        <el-form-item label="商品品类:" prop="kind">
          <el-radio-group v-model="ruleForm.kind">
            <el-radio
              border
              label="上衣"
              @change="ruleForm.show = 1"
            ></el-radio>
            <el-radio
              border
              label="下装"
              @change="ruleForm.show = 2"
            ></el-radio>
            <el-radio
              border
              label="鞋类"
              @change="ruleForm.show = 3"
            ></el-radio>
            <el-radio
              border
              label="饰品"
              @change="ruleForm.show = 4"
            ></el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="商品种类" v-show="ruleForm.show == 1" prop="type">
          <el-checkbox-group v-model="ruleForm.type">
            <el-checkbox-button
              v-for="(type, i) in this.type[0]['上衣']"
              :key="i"
              :label="type"
              name="type"
            ></el-checkbox-button>
          </el-checkbox-group>
        </el-form-item>
        <el-form-item label="商品种类" v-show="ruleForm.show == 2" prop="type">
          <el-checkbox-group v-model="ruleForm.type">
            <el-checkbox-button
              v-for="(type, i) in this.type[0]['下装']"
              :key="i"
              :label="type"
              name="type"
            ></el-checkbox-button>
          </el-checkbox-group>
        </el-form-item>
        <el-form-item label="商品种类" v-show="ruleForm.show == 3" prop="type">
          <el-checkbox-group v-model="ruleForm.type">
            <el-checkbox-button
              v-for="(type, i) in this.type[0]['鞋类']"
              :key="i"
              :label="type"
              name="type"
            ></el-checkbox-button>
          </el-checkbox-group>
        </el-form-item>
        <el-form-item label="商品种类" v-show="ruleForm.show == 4" prop="type">
          <el-checkbox-group v-model="ruleForm.type">
            <el-checkbox-button
              v-for="(type, i) in this.type[0]['饰品']"
              :key="i"
              :label="type"
              name="type"
            ></el-checkbox-button>
          </el-checkbox-group>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')"
            >立即发布</el-button
          >
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-main>
  </div>
</template>
<script>
export default {
  data() {
    return {
      upurl: "http://localhost:8000/stroll/upload",
      imageUrl: [],
      type: [
        {
          下装: [
            "束腿裤",
            "紧身裤",
            "牛仔裤",
            "短裤",
            "西裤",
            "修身裤",
            "内裤",
          ],
          上衣: [
            "夹克",
            "t恤",
            "西服",
            "汗衫",
            "毛衫",
            "褂子",
            "卫衣",
            "棒球服",
            "POLO衫",
            "羽绒服",
            "棉服",
          ],
          饰品: [
            "手表",
            "帽子",
            "项链",
            "耳坠",
            "戒指",
            "纹身贴",
            "眼镜",
            "手链",
            "手环",
          ],
          鞋类: [
            "篮球鞋",
            "皮鞋",
            "帆布鞋",
            "豆豆鞋",
            "运动鞋",
            "马丁靴",
            "雨鞋",
            "板鞋",
          ],
        },
      ],
      dialogImageUrl: "",
      dialogVisible: false,
      ruleForm: {
        title: "",
        commodity: "",
        price: "",
        inventory: "",
        kind: "",
        type: [],
        show: 1,
      },
      rules: {
        title: [
          { required: true, message: "请输入商品名称", trigger: "blur" },
          {
            min: 0,
            max: 10,
            message: "长度在 0 到 10 个字符",
            trigger: "blur",
          },
        ],
        commodity: [
          { required: true, message: "请输入商品介绍", trigger: "blur" },
        ],
        price: [
          {
            required: true,
            message: "请输入价格",
            trigger: "blur",
          },
          {
            type: "number",
            message: "输入格式只能为数字",
            trigger: "blur",
          },
        ],
        count: [
          {
            required: true,
            message: "请输入上架数量",
            trigger: "blur",
          },
          {
            type: "number",
            message: "输入格式只能为数字",
            trigger: "blur",
          },
        ],
        kind: [
          {
            required: true,
            message: "请选择品类",
            trigger: "change",
          },
        ],
        type: [
          {
            required: true,
            message: "请选择种类",
            trigger: "change",
          },
        ],
      },
    };
  },
  methods: {
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    success(response) {
      this.imageUrl.push(response);
    },
    handleExceed(files) {
      this.$message.warning(`当前限制只能选择${files.length} 个文件`);
    },
    beforeRemove(file) {
      return this.$confirm(`确定移除 ${file.name}`);
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = "stroll/upinfo";
          let { title, commodity, price, inventory, kind, type } =
            this.ruleForm;
          let data = `title=${title}&commodity=${commodity}&price=${price}&inventory=${inventory}&kind=${kind}&type=${type}&img=${
            this.imageUrl
          }&shopId=${sessionStorage.getItem("shopId")}`;
          this.axios.post(url, data).then((res) => {
            if (res) {
              this.$message("提交成功");
              this.clearFiles();
              this.resetForm(formName);
              this.imageUrl = [];
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    clearFiles() {
      this.$refs["upload"].clearFiles();
    },
  },
};
</script>

<style lang="scss" scoped>
div {
  line-height: 0;
}
.upload-demo {
  height: 180px;
  text-align: center;
}
</style>
